package com.lebang.service.cences;

import com.lebang.domain.CencesSdkErrorLog;
import com.lebang.domain.EmailSdkerrorSendRecord;
import com.lebang.example.CencesSdkErrorLogExample;
import com.lebang.mapper.CencesSdkErrorLogMapper;
import com.lebang.mapper.EmailSdkerrorSendRecordMapper;
import com.lebang.mapper.SdkErrorLogMapper;
import com.lebang.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
* 
* @Author ljx
* @Date 2020-12-08
*/
@Service
public class CencesSdkErrorLogService extends BaseService<CencesSdkErrorLog,Integer, CencesSdkErrorLogExample>{
    @Autowired
    private SdkErrorLogMapper sdkErrorLogMapper;
    @Autowired
    private CencesSdkErrorLogMapper mapper;
    //@Autowired
  //  private JavaMailSender javaMailSender;
    @Autowired
    private EmailSdkerrorSendRecordMapper emailSdkerrorSendRecordMapper;
    @Value("${info.manager-background}")
    public String MANAGER_BACKGROUND;
    @Value("${info.name}")
    public String appName;

    public void starDataInit(LocalDateTime end) {
        this.starDataInit(end, null);
    }

    /**
     * 信息上报统计
     * @param end
     * @param sdkKey
     */
    public void starDataInit(LocalDateTime end, List<String> sdkKey) {

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime star = end.minusDays(1);

        //统计日期
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));

        //根据日期统计
        List<CencesSdkErrorLog> list=sdkErrorLogMapper.countByTime(star,end);

        for (CencesSdkErrorLog cencesSdkErrorLog : list) {
            cencesSdkErrorLog.setCreateTime(LocalDateTime.now());
            cencesSdkErrorLog.setTime(time);
            mapper.insertSelective(cencesSdkErrorLog);
        }
    }

    public List<CencesSdkErrorLog> selectTime(Integer page, Integer limit, Integer star, Integer end) {
        return mapper.selectTime(page,limit,star,end);
    }

    public Integer countTime(Integer page, Integer limit, Integer star, Integer end) {
        return mapper.countTime(page,limit,star,end);
    }

    /**
     * 发邮件
     */
    public void sendEmail(List<EmailSdkerrorSendRecord> list,LocalDateTime end) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        SimpleMailMessage message =new SimpleMailMessage();
        message.setFrom("service@gara.cc");
        message.setTo("178791359@qq.com","313966390@qq.com","781050495@qq.com");
        message.setSubject(end.format(df) +appName+"【错误码警告】");

        StringBuilder innerText = new StringBuilder();
        for (EmailSdkerrorSendRecord record : list) {
            String text = "错误数:" + record.getErrorCount()+ "\n"
                    + "数据源:" + record.getSdkKey()+ "\n"
                    +"错误码:"+record.getErrorCode()+ "\n"
                    +"错误信息:"+  record.getErrorMessage()+ "\n"+"\n";
            innerText.append(text);
        }
        message.setText(innerText.toString());
        //javaMailSender.send(message);
        for (EmailSdkerrorSendRecord record : list) {
            emailSdkerrorSendRecordMapper.insertSelective(record);
        }
    }

    /**
     * 信息上报详情
     * @param page
     * @param limit
     * @param time
     * @return
     */
    public List<CencesSdkErrorLog> getLookList(Integer page, Integer limit, Integer time) {
        return mapper.getLookList(page,limit,time);
    }

    public Integer countLookList(Integer time) {
        return mapper.countLookList(time);
    }

    /**
     * 报错位置
     * @param page
     * @param limit
     * @param time
     * @param sdkKey
     * @param errorCode
     * @param errorMsg
     * @return
     */
    public List<CencesSdkErrorLog> getErrorLookList(Integer page, Integer limit, Integer time, String sdkKey, String errorCode, String errorMsg) {
        return mapper.getErrorLookList(page,limit,time,sdkKey,errorCode,errorMsg);
    }

    public Integer countErrorLookList(Integer time,String sdkKey, String errorCode, String errorMsg) {
        return mapper.countErrorLookList(time,sdkKey,errorCode,errorMsg);
    }

    /**
     * 删除
     * @param time
     * @return
     */
    public int deleteByTime(Integer time) {
        return mapper.deleteByTime(time);
    }
}
